package com.muye.dianli.activity;

import java.io.File;
import java.nio.charset.Charset;
import java.util.ArrayList;

import org.apache.http.entity.mime.content.StringBody;

import android.annotation.SuppressLint;
import android.content.DialogInterface;
import android.content.DialogInterface.OnKeyListener;
import android.graphics.PixelFormat;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.util.Log;
import android.view.KeyEvent;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.animation.Animation;
import android.view.animation.Animation.AnimationListener;
import android.view.animation.AnimationUtils;
import android.view.animation.LinearInterpolator;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.google.gson.Gson;
import com.muye.constant.C;
import com.muye.constant.ConstantPersonalInfo;
import com.muye.constant.U;
import com.muye.dianli.BaseActivity;
import com.muye.dianli.R;
import com.muye.field.MemberCardData;
import com.muye.field.MemberCardField;
import com.muye.field.MemberCardPath;
import com.tangchao.view.VerticalViewCharacterHorizontal;
import com.zvezda.algorithm.utils.MD5;
import com.zvezda.android.utils.BaseUtils;
import com.zvezda.disply.utils.DisplayConfig;
import com.zvezda.disply.utils.DisplayConst;
import com.zvezda.disply.utils.DisplayManager;
import com.zvezda.http.utils.HttpConst;
import com.zvezda.http.utils.KeyValue;
import com.zvezda.http.utils.RequestHttpListener;
import com.zvezda.http.utils.RequestHttpPostThread;

/**
 * 我的会员卡
 * 
 * @author TangChao
 *
 */
public class MemberCardActivity extends BaseActivity implements OnClickListener, OnKeyListener, AnimationListener
{
	/**
	 * 请求线程
	 */
	private RequestHttpPostThread requestHttpPostThread;
	
	/**
	 * 会员卡返回字段类
	 */
	private MemberCardData memberCardData;
	
	/**
	 * 会员卡返回路劲
	 */
	private MemberCardPath memberCardPath;
	
	/**
	 * 会员卡正面布局
	 */
	private RelativeLayout before;
	
	/**
	 * 会员卡反面布局
	 */
	private RelativeLayout after;
	
	/**
	 * 会员卡正面
	 */
	private ImageView memberCardImageViewBefore;
	
	/**
	 * 会员卡反面
	 */
	private ImageView memberCardImageViewAfter;
	
	/**
	 * 会员卡反面二维码
	 */
	private ImageView twoDimensionCode;
	
	/**
	 * 会员卡反面文字
	 */
	private VerticalViewCharacterHorizontal memberCardAfterText;
	
	/**
	 * 动画
	 */
	private Animation animation;
	
	/**
	 * 沿Y轴翻转动画
	 */
	// private RorateWithY animation;
	
	/**
	 * 会员卡本地存储路径
	 */
	private String filePath;
	
	@Override
	protected void onCreate(Bundle savedInstanceState)
	{
		super.onCreate(savedInstanceState);
		getWindow().setFormat(PixelFormat.RGBA_8888);
		setContentView(R.layout.member_card_layout);
		filePath = C.FILE_PATH + "/DianLi/MemberCard";
		File file = new File(filePath);
		if (!file.exists())
		{
			file.mkdirs();
		}
		createTitle();
		initWidget();
		getMemberCard();
	}
	
	@Override
	protected void onDestroy()
	{
		super.onDestroy();
	}
	
	/**
	 * 初始化标题栏
	 */
	private void createTitle()
	{
		TextView titleName = (TextView) findViewById(R.id.title_bar_name_text_view);
		titleName.setText("会员卡");
		titleName.setTextColor(getResources().getColor(R.color.white));
		ImageView leftBtn = (ImageView) findViewById(R.id.title_bar_left_image_view);
		leftBtn.setImageResource(R.drawable.title_back_icon);
		leftBtn.setVisibility(View.VISIBLE);
		leftBtn.setOnClickListener(this);
	}
	
	/**
	 * 
	 * 初始化控件
	 */
	private void initWidget()
	{
		animation = AnimationUtils.loadAnimation(this, R.anim.member_card_animation);
		// animation = new RorateWithY(true);
		LinearInterpolator linearInterpolator = new LinearInterpolator();
		animation.setInterpolator(linearInterpolator);
		animation.setAnimationListener(this);
		/* ========================= */
		before = (RelativeLayout) findViewById(R.id.member_ard_layout_before);
		before.setOnClickListener(this);
		after = (RelativeLayout) findViewById(R.id.member_ard_layout_after);
		after.setOnClickListener(this);
		before.setVisibility(View.VISIBLE);
		after.setVisibility(View.GONE);
		/* ===================================== */
		memberCardImageViewBefore = (ImageView) findViewById(R.id.member_card_layout_image_view_before);
		memberCardImageViewBefore.setAnimation(animation);
		memberCardImageViewAfter = (ImageView) findViewById(R.id.member_card_layout_image_view_after);
		twoDimensionCode = (ImageView) findViewById(R.id.member_card_layout_image_view_after_code_image);
		memberCardAfterText = (VerticalViewCharacterHorizontal) findViewById(R.id.member_card_layout_image_view_after_text);
	}
	
	/**
	 * 请求会员卡
	 */
	private void getMemberCard()
	{
		C.openPragressDialog(this, this, R.string.in_request_data);
		ArrayList<KeyValue<String, Object>> paramsList = new ArrayList<KeyValue<String, Object>>();
		String str = C.APPID + ConstantPersonalInfo.uid + C.CONSTANT;
		try
		{
			paramsList.add(new KeyValue<String, Object>("uid", new StringBody(ConstantPersonalInfo.uid, Charset
					.forName("utf-8"))));
			paramsList.add(new KeyValue<String, Object>("appId", new StringBody(C.APPID, Charset.forName("utf-8"))));
			paramsList
					.add(new KeyValue<String, Object>("sign", new StringBody(MD5.getMD5_32(str), Charset.forName("utf-8"))));
		}
		catch (Exception e)
		{
			e.printStackTrace();
		}
		requestHttpPostThread = new RequestHttpPostThread(getMemberCardListener, U.GET_MEMBER_CARD, paramsList);
		requestHttpPostThread.start();
	}
	
	/**
	 * 请求会员卡返回监听
	 */
	private RequestHttpListener getMemberCardListener = new RequestHttpListener()
	{
		
		@Override
		public void requestHttp(Message msg)
		{
			Bundle bundle = msg.getData();
			int result = bundle.getInt(HttpConst.ACTION_RESULT, HttpConst.ACTION_FAIL);
			if (result == HttpConst.ACTION_SUCCESS)
			{
				byte[] dataArr = bundle.getByteArray(HttpConst.DATA_ARR);
				try
				{
					String str = new String(dataArr, "utf-8");
					Log.i(C.TAG, "会员卡请求结果----->" + str);
					Gson gson = new Gson();
					MemberCardField iteMemberCardField = gson.fromJson(str, MemberCardField.class);
					memberCardData = iteMemberCardField.data;
					memberCardPath = memberCardData.pictureUrl;
				}
				catch (Exception e)
				{
					e.printStackTrace();
				}
				Message message = new Message();
				message.what = 0;
				message.arg1 = result;
				handler.sendMessage(message);
			}
		}
	};
	
	/**
	 * 处理数据
	 */
	private Handler handler = new Handler()
	{
		
		@SuppressLint("WrongCall")
		@Override
		public void handleMessage(Message msg)
		{
			super.handleMessage(msg);
			C.closePragressDialog();
			switch (msg.what)
			{
				case 0:
					switch (msg.arg1)
					{
						case HttpConst.ACTION_SUCCESS:
							DisplayManager.getInstance().open(MemberCardActivity.this);
							// 正面
							DisplayConfig displayConfigBefore = new DisplayConfig();
							displayConfigBefore.url = memberCardPath.before;
							displayConfigBefore.path = BaseUtils.getFilePath(filePath, memberCardPath.before);
							displayConfigBefore.imageView = memberCardImageViewBefore;
							displayConfigBefore.w = 540;
							displayConfigBefore.h = 970;
							displayConfigBefore.zoomModel = DisplayConst.ZOOM_FULL;
							DisplayManager.getInstance().display(displayConfigBefore);
							// 反面
							DisplayConfig displayConfigAfter = new DisplayConfig();
							displayConfigAfter.url = memberCardPath.after;
							displayConfigAfter.path = BaseUtils.getFilePath(filePath, memberCardPath.after);
							displayConfigAfter.imageView = memberCardImageViewAfter;
							displayConfigAfter.w = 540;
							displayConfigAfter.h = 970;
							displayConfigAfter.zoomModel = DisplayConst.ZOOM_FULL;
							DisplayManager.getInstance().display(displayConfigAfter);
							// 二维码
							DisplayConfig displayConfigCode = new DisplayConfig();
							displayConfigCode.url = memberCardData.qrcode;
							displayConfigCode.path = BaseUtils.getFilePath(filePath, memberCardData.qrcode);
							displayConfigCode.imageView = twoDimensionCode;
							displayConfigCode.w = 299;
							displayConfigCode.h = 299;
							displayConfigCode.zoomModel = DisplayConst.ZOOM_FULL;
							DisplayManager.getInstance().display(displayConfigCode);
							memberCardAfterText.setText(memberCardData.desc);
							break;
						case HttpConst.ACTION_FAIL:
							C.showToast(MemberCardActivity.this, "请求失败,请重试");
							break;
						
						default:
							break;
					}
					break;
				
				default:
					break;
			}
		}
		
	};
	
	@Override
	public void onClick(View v)
	{
		switch (v.getId())
		{
			case R.id.title_bar_left_image_view:
				finish();
				break;
			case R.id.member_ard_layout_before:// 正面
				if (before.isShown())
				{
					before.startAnimation(animation);
					before.setVisibility(View.GONE);
					after.setVisibility(View.VISIBLE);
				}
				break;
			case R.id.member_ard_layout_after:// 反面
				if (after.isShown())
				{
					after.startAnimation(animation);
					after.setVisibility(View.GONE);
					before.setVisibility(View.VISIBLE);
				}
				break;
			default:
				break;
		}
	}
	
	@Override
	public boolean onKey(DialogInterface dialog, int keyCode, KeyEvent event)
	{
		if (requestHttpPostThread != null)
		{
			requestHttpPostThread.interrupt();
			requestHttpPostThread.setRequestHttpListener(null);
		}
		return false;
	}
	
	@Override
	public void onAnimationStart(Animation animation)
	{
		before.setEnabled(false);
		after.setEnabled(false);
		// if (before.getVisibility() == View.GONE)
		// {
		// after.setVisibility(View.VISIBLE);
		// }
		// else if (after.getVisibility() == View.GONE)
		// {
		// before.setVisibility(View.VISIBLE);
		// }
	}
	
	@Override
	public void onAnimationEnd(Animation animation)
	{
		before.setEnabled(true);
		after.setEnabled(true);
		// if (before.getVisibility() == View.GONE)
		// {
		// after.setVisibility(View.VISIBLE);
		// }
		// else if (after.getVisibility() == View.GONE)
		// {
		// before.setVisibility(View.VISIBLE);
		// }
	}
	
	@Override
	public void onAnimationRepeat(Animation animation)
	{
		
	}
	
}
